package com.example.liwushuo.fragment;

import java.security.PublicKey;
import java.util.ArrayList;
import java.util.List;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.liwushuo.GVInfoActivity;
import com.example.liwushuo.HorizontalListView;
import com.example.liwushuo.HorzontalScrollActivity;
import com.example.liwushuo.R;
import com.example.liwushuo.TopAllActivity;
import com.example.liwushuo.TopAllInfoActivity;
import com.example.liwushuo.adapter.GridVieAdapter;
import com.example.liwushuo.adapter.GridViewAdapter;
import com.example.liwushuo.adapter.HorizontalListViewAdapter;
import com.example.liwushuo.bar.SelfBar;
import com.example.liwushuo.bar.SortBar;
import com.example.liwushuo.bean.BaseBean;
import com.example.liwushuo.bean.Channel_groups;
import com.example.liwushuo.bean.Channels;
import com.example.liwushuo.bean.Collections;
import com.example.liwushuo.urls.UrlConfig;
import com.example.liwushuo.utils.GridViewWithHeaderAndFooter;
import com.lidroid.xutils.BitmapUtils;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.GestureDetector.OnGestureListener;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.webkit.WebView.FindListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

/**
 * @author
 * 
 *         这是分类的碎片
 */
public class SortFragment extends Fragment implements OnGestureListener {

	/*
	 * 
	 * 顶部水平滑动的控件
	 */
	private List<Collections> list = new ArrayList<Collections>();
	private HorizontalListViewAdapter hlva;
	private HorizontalListView hlv;
	public int id;
	private TextView textView, gridview_title1, title;
	/*
	 * GirdView
	 */
	// private GridView gridView;

	private List<Channels> channels;
	private GridVieAdapter gridViewAdapter;
	private List<Channel_groups> groups;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		View view = inflater.inflate(R.layout.sort_fragment, null);

		getFragmentManager().beginTransaction()
				.replace(R.id.sort_bar_id, new SortBar()).commit();
		/*
		 * 初始化水平滚动的控件
		 */
		// gridview_title1=(TextView) view.findViewById(R.id.gridview_title1);
		// gridview_title1.setText("品类");
		textView = (TextView) view.findViewById(R.id.all_look);
		textView.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(getActivity(), TopAllActivity.class);
				startActivity(intent);

			}
		});
		hlv = (HorizontalListView) view.findViewById(R.id.horizontallistview1);
		hlva = new HorizontalListViewAdapter(getActivity(), list);
		hlva.notifyDataSetChanged();

		hlv.setAdapter(hlva);

		hlv.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {

				id = list.get(arg2).getId();

				// TODO Auto-generated method stub
				Intent intent = new Intent(getActivity(),
						HorzontalScrollActivity.class);
				intent.putExtra("id", id);
				startActivity(intent);

			}
		});
		/*
		 * 初始化GridView控件
		 */
		// gridView = (GridView) view.findViewById(R.id.gridview_layout);
		// channels = new ArrayList<Channels>();
		// gridViewAdapter = new GridViewAdapter(getActivity(), channels);
		//
		// gridView.setAdapter(gridViewAdapter);
		GridViewWithHeaderAndFooter gridView = (GridViewWithHeaderAndFooter) view
				.findViewById(R.id.gridview_layout);

		LayoutInflater layoutInflater = LayoutInflater.from(getActivity());
		View headerView = layoutInflater.inflate(R.layout.headview, null);
		title = (TextView) headerView.findViewById(R.id.gridview_title1);

		gridView.addHeaderView(headerView);
		channels = new ArrayList<Channels>();
		gridViewAdapter = new GridVieAdapter(getActivity(), channels);
		gridView.setAdapter(gridViewAdapter);

		gridView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				// TODO Auto-generated method stub
				//Toast.makeText(getActivity(), "跳转", 1).show();
				int id = channels.get(arg2).getId();
				Intent intent = new Intent(getActivity(),
						GVInfoActivity.class);
				intent.putExtra("id", id);
				startActivity(intent);
			}
		});
		// gridView.addFooterView(footerView);
		// 加载品类的数据
		initSort();
		// 加载水平滚动的图片
		initData();
		return view;

	}

	/*
	 * 
	 * 加载分类数据
	 */
	private void initSort() {
		// TODO Auto-generated method stub
		HttpUtils httpUtils = new HttpUtils();
		httpUtils.send(HttpMethod.GET, UrlConfig.URL_CATEGORY,
				new RequestCallBack<String>() {

					@Override
					public void onFailure(HttpException arg0, String arg1) {
						// TODO Auto-generated method stub

					}

					@Override
					public void onSuccess(ResponseInfo<String> responseInfo) {
						// TODO Auto-generated method stub

						String json = responseInfo.result;
						Log.i("json", "=========" + json);
						BaseBean baseBean = JSONObject.parseObject(json,
								BaseBean.class);

						List<Channel_groups> groups = baseBean.getData()
								.getChannel_groups();

						for (Channel_groups channel_groups : groups) {
							int id = channel_groups.getId();
							String name = channel_groups.getName();
							// if (id == 5 && name.equals("品类")) {

							title.setText("礼物说");
							List<Channels> list1 = channel_groups.getChannels();

							channels.addAll(list1);

						}

					}
				});
	}

	/*
	 * 
	 * 
	 * 加载水平滚动图片的信息
	 */
	private void initData() {
		HttpUtils httpUtils = new HttpUtils();
		httpUtils.send(HttpMethod.GET, UrlConfig.URL_HOR,
				new RequestCallBack<String>() {

					@Override
					public void onFailure(HttpException arg0, String arg1) {
						// TODO Auto-generated method stub

					}

					@Override
					public void onSuccess(ResponseInfo<String> responseInfo) {
						// TODO Auto-generated method stub
						String json = responseInfo.result;
						BaseBean baseBean = JSONObject.parseObject(json,
								BaseBean.class);
						List<Collections> list2 = baseBean.getData()
								.getCollections();

						list.addAll(list2);

					}
				});

	}

	private final String TAG = "HorizontalListViewActivity";
	private HttpUtils httpUtils;

	@Override
	public boolean onDown(MotionEvent e) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
			float velocityY) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public void onLongPress(MotionEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
			float distanceY) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public void onShowPress(MotionEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public boolean onSingleTapUp(MotionEvent e) {
		// TODO Auto-generated method stub
		return false;
	}

}
